package com.logisticsmanagement.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logisticsmanagement.services.UserService;
import com.logisticsmanagement.utils.GlobelValue;
import com.logisticsmanagement.utils.JsonUtls;
import com.logisticsmanagement.vo.BaseResponse;
import com.logisticsmanagement.vo.VoUser;

import net.sf.json.JSONObject;

@WebServlet("/User")
public class UserServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		UserService service = new UserService();
		JSONObject responseJson = null;
		
		JSONObject jsonObject = JsonUtls.JSONObject(req);
		
		String requetsMothod = "";
		System.out.println(jsonObject.toString());
		try{
			requetsMothod = jsonObject.getString(GlobelValue.REQUEST_METHOD);
		}catch(Exception e){
			 responseJson = JSONObject.fromObject(new BaseResponse<VoUser>(GlobelValue.ERROR_LOST_REQUEST_MOTHOD,GlobelValue.ERROR_LOST_REQUEST_MOTHOD_DESCRIPTION));
			String response = responseJson.toString();
			System.out.println(response);
			 resp.getWriter().write(response);
			 return;
		}

		System.out.println(requetsMothod);
		if(requetsMothod.equals(GlobelValue.METHOD_ADD)){//registor
			JSONObject registorUser = jsonObject.getJSONObject("user");
			responseJson = JSONObject.fromObject(service.register(Json2VoUser(registorUser)));
		}else if(requetsMothod.equals(GlobelValue.METHOD_LOGIN)){//login
			String userName = jsonObject.getString("userName");
			String password = jsonObject.getString("pssword");
			responseJson = JSONObject.fromObject(service.login(userName, password));
		}else if(requetsMothod.equals(GlobelValue.METHOD_GETINFO)){//get one user information
			responseJson = JSONObject.fromObject(service.getUserInfo(jsonObject.getString("userID")));
		}else if(requetsMothod.equals(GlobelValue.METHOD_GETLIST)){//get users which managed by this city or delivery station
			int pageNow = jsonObject.getInt("pageNow");
			if(pageNow<0){
				pageNow =0;
			}
			
			int pageSize = jsonObject.getInt("pageSize");
			if(pageSize<0){
				pageSize =0;
			}
			
			String userName = jsonObject.getString("userName");
			VoUser voRequirement = null;
			
			JSONObject jsonRequirement = jsonObject.getJSONObject("voRequirement");
			if(jsonRequirement != null){
				voRequirement = (VoUser) JSONObject.toBean(jsonRequirement, VoUser.class);
			}else{
				voRequirement = new VoUser();
			}
			
			VoUser opreator = new VoUser();
			opreator.setUserID(userName);
			
			responseJson = JSONObject.fromObject(service.getList(voRequirement, opreator, pageNow, pageSize));
		}else{
			//request method not exit
			responseJson = JSONObject.fromObject(new BaseResponse<VoUser>(GlobelValue.ERROR_NOT_EXIST_REQUEST_MOTHOD,GlobelValue.ERROR_NOT_EXIST_REQUEST_MOTHOD_DESCRIPTION));
		}
		resp.getWriter().write(responseJson.toString());
	}
	
	private VoUser Json2VoUser(JSONObject inputJson){
		VoUser voUser = new VoUser();
		voUser.setAddress(inputJson.getString("address"));
		voUser.setDeliveryStationID(inputJson.getInt("deliveryStationID"));
		voUser.setDisplayName(inputJson.getString("displayName"));
		voUser.setGender(inputJson.getInt("gender"));
		voUser.setMobilePhone(inputJson.getString("mobilePhone"));
		voUser.setPassword(inputJson.getString("password"));
		voUser.setRole(inputJson.getString("role"));
		voUser.setUserID(inputJson.getString("userID"));
		voUser.setDeliveryStationName(inputJson.getString("deliveryStationName"));
		
		return voUser;
	}
}
